/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 河北
 * 排名: 完成
 * 去年排名：完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-22
 */
public class Hebei extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("15130802110218", "XAPRJUT5", "130825199708030020", "234", BEN_KE_LI_KE);
		Hebei gansu = new Hebei(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public Hebei(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		delCookie();
		Request captchaRequest = new Request("http://xxcx.hebeea.edu.cn/hebeeaQuery/VerificationCode.jpg", mSha1);
		captchaRequest.addHeader("Referer", "http://xxcx.hebeea.edu.cn/hebeeaQuery/pages/uee/cjcx/cjcx.jsp");
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://xxcx.hebeea.edu.cn/hebeeaQuery/uee/cjcx", mSha1);
		loginRequest.setCharSet("utf-8");
		loginRequest.addHeader("Referer", "http://xxcx.hebeea.edu.cn/hebeeaQuery/pages/uee/cjcx/cjcx.jsp");
		// loginRequest.addHeader("Host", "xxcx.hebeea.edu.cn");
		// loginRequest.addHeader("Origin", "http://xxcx.hebeea.edu.cn");
		loginRequest.addParams("uee_ksh", mStudent.getStudentId());
		loginRequest.addParams("uee_zjhm", mStudent.getIdCard());
		loginRequest.addParams("uee_dlh", mStudent.getStudentPwd());
		loginRequest.addParams("kaptchaCode", captcha);
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("个人信息"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("未找到查询结果"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("考生号") || loginHtml.contains("登录号"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("验证码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();

		Elements trsif = loginDocument.select("table.ks_table").get(0).select("tr");
		Elements trssc = loginDocument.select("table.score_table").get(0).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(2).select("td").get(1).text().trim());
		result.put("ranking", getRank(10 + 31, trssc.get(5).select("td").get(1).text().trim(), mStudent.getType()));
		result.put("province", "河北省");

		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trssc.get(1).select("td").get(1).text().trim());
		scoreObject.put("数学", trssc.get(2).select("td").get(1).text().trim());
		scoreObject.put("英语", trssc.get(3).select("td").get(1).text().trim());
		scoreObject.put("综合", trssc.get(4).select("td").get(1).text().trim());
		scoreObject.put("总分", trssc.get(5).select("td").get(1).text().trim());

		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(10 + 31);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2015/124063.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2015/124062_3.html"));
		// 文科 http://www.gxeduw.com/news/2015/124062.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 118823;
		int asSize = as.size();
		for (int i = 2; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(2).text(), "(\\d+)");

			if (countString.equals(""))
				continue;
			Integer count = Integer.parseInt(countString);
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
			System.out.println(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(10);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2015/124063.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/107027_3.html"));
		// 文科 http://www.gxeduw.com/news/2015/124062.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 137054;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			String countString = regexMatch(as.get(i).select("td").get(4).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(5).text(), "(\\d+)");

			if (countString.equals(""))
				continue;
			Integer count = Integer.parseInt(countString);
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
			System.out.println(rank);
		}
	}
}
